package com.jiujiu;

import com.google.common.base.Preconditions;
import com.jiujiu.MustInterface.JiuJiuAnalysis;
import com.jiujiu.analysisFile.TemplateAnalysis;
import com.jiujiu.common.Constant;

import com.jiujiu.constant.ModelEnum;
import com.jiujiu.constant.PathConstant;
import com.jiujiu.utitls.MyFiles;
import com.jiujiu.utitls.PropertyAnalysis;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Map;

/**
 * @author ZhaoJunZhou
 * @Date 2020/10/9
 * 入口函数
 * 1 解析freemarker 的路径
 *  默认读取的路径为resource下的sqlmodel文件可以自定义
 * 2 读取生成一个或者多个的entity，在freemarker中
 * 3 根据每个模板所指定的位置读取其余需要生成的东西
 *
 *
 *
 */
public class Main {
    public static final Logger log = Logger.getLogger(Main.class);
    /**默认的读取位置*/
    public static String templatePath ;
    private static Map<String, String> property;
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        property = PropertyAnalysis.fromArgs(args);
        String model = property.get("model").toUpperCase();
        templatePath = initTemplatePath(args);
        Preconditions.checkArgument(StringUtils.isNotBlank(model),"请指定你的转换模式");
        ModelEnum modelEnum = null;
        boolean next = true;
        try {
            modelEnum=  ModelEnum.valueOf(model);
        } catch (IllegalArgumentException e) {
            next = false;
            log.info("未找到指定的类型");
        }
        if (next){
            /**
             * 使用类加载的方式进行处理
             */
            Class analysisTarget = Class.forName(modelEnum.getClassName());
            JiuJiuAnalysis jiuJiuAnalysis= (JiuJiuAnalysis) analysisTarget.newInstance();
            File sqlModelFile = MyFiles.getFile(templatePath);
            new TemplateAnalysis(sqlModelFile, jiuJiuAnalysis).analysis();
        }
    }
    /**
     * 设置默认的读取位置
     * --model_path path
     * */
    public static String initTemplatePath(String[] args) {
        Map<String,String> prop = PropertyAnalysis.fromArgs(args);
        if (prop.containsKey(Constant.READ_FILE_PATH)) {
            log.info(String.format("读取到model_path为：%s",templatePath));
            return prop.get(Constant.READ_FILE_PATH);
        }else {
            log.info("没有指定读取的model_path的路径,使用默认值：" + PathConstant.JSON_PATH);
            return PathConstant.JSON_PATH;
        }
    }
}
